/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Form;

import Boundary.*;
import Control.*;
import Table.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;

/**
 *
 * @author Fave
 */
public class GuruForm extends javax.swing.JFrame {

    String jam;
    String menit;
    String detik;

    /** Creates new form LoginForm */
    public GuruForm() {
        setResizable(false);
        setTitle("Sistem Presensi Akademik SMS Gateway");
        try {
            initComponents();
            GregorianCalendar gc = new GregorianCalendar();
            date_chooser.setDate(gc.getTime());
            date_chooser.setEnabled(false);
            Date tgl = this.date_chooser.getDate();
            SimpleDateFormat tglsimple = new SimpleDateFormat("dd-MM-yyyy");
            String tanggal = tglsimple.format(tgl);
            System.out.println(tanggal);
            String kelas = kelasCombo.getSelectedItem().toString();
            List<Guru> gr = (List<Guru>) GuruKontrol.getKoneksi().getAll(tanggal, kelas);
            GuruTabelModel model = new GuruTabelModel(gr);
            TabelPresensi.setModel(model);
        } catch (Exception e) {
            Logger.getLogger(GuruForm.class.getName()).log(Level.SEVERE, null, e);
        }

        // Timer
        new Timer(1000, taskPerformer).start();
    }
    ActionListener taskPerformer = new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            String nol_jam = "";
            String nol_menit = "";
            String nol_detik = "";
            // Membuat Date
            Date dt = new Date();
            // Mengambil nilai JAM, MENIT, dan DETIK Sekarang

            int nilai_jam = dt.getHours();
            int nilai_menit = dt.getMinutes();
            int nilai_detik = dt.getSeconds();
            // Jika nilai JAM lebih kecil dari 10 (hanya 1 digit)
            if (nilai_jam <= 9) {
                nol_jam = "0";
            }
            // Jika nilai MENIT lebih kecil dari 10 (hanya 1 digit)
            if (nilai_menit <= 9) {
                nol_menit = "0";
            }
            // Jika nilai DETIK lebih kecil dari 10 (hanya 1 digit)
            if (nilai_detik <= 9) {
                nol_detik = "0";
            }

            jam = nol_jam + Integer.toString(nilai_jam);
            menit = nol_menit + Integer.toString(nilai_menit);
            detik = nol_detik + Integer.toString(nilai_detik);
            // Menampilkan pada Layar
            jamfield.setText("  " + jam + " : " + menit + " : " + detik + "  ");
        }
    };

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        TabelPresensi = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        Editbutton = new javax.swing.JToggleButton();
        KeluarButton = new javax.swing.JToggleButton();
        jamfield = new javax.swing.JLabel();
        date_chooser = new com.toedter.calendar.JDateChooser();
        Refreshbutton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        kelasCombo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Data Presensi Harian Siswa", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(0, 0, 0))); // NOI18N

        TabelPresensi.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "NIS", "Nama", "Status"
            }
        ));
        jScrollPane2.setViewportView(TabelPresensi);

        jLabel2.setText("Tanggal");

        Editbutton.setText("Edit");
        Editbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditbuttonActionPerformed(evt);
            }
        });

        KeluarButton.setText("Keluar");
        KeluarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KeluarButtonActionPerformed(evt);
            }
        });

        jamfield.setFont(new java.awt.Font("Tahoma", 1, 18));
        jamfield.setText("00:00:00");

        date_chooser.setBackground(new java.awt.Color(255, 255, 255));
        date_chooser.setForeground(new java.awt.Color(255, 255, 255));
        date_chooser.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        Refreshbutton.setText("Refresh");
        Refreshbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RefreshbuttonActionPerformed(evt);
            }
        });

        jLabel3.setText("Kelas");

        kelasCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(date_chooser, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(kelasCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(59, 59, 59)
                        .addComponent(Refreshbutton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jamfield, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(Editbutton, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(KeluarButton))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 742, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)))
                .addGap(35, 35, 35))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(date_chooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(kelasCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(Refreshbutton))
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jamfield, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(KeluarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Editbutton, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 138, 810, -1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/banner1.jpg"))); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 810, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void EditbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditbuttonActionPerformed
        EditGuru eg = new EditGuru(this, true);
        eg.setVisible(true);

        //   JOptionPane.showMessageDialog(null,jam + menit + detik);

    }//GEN-LAST:event_EditbuttonActionPerformed

    private void KeluarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KeluarButtonActionPerformed
        LoginForm lf = new LoginForm();
        //   lf.setVisible(true);
        dispose();
    }//GEN-LAST:event_KeluarButtonActionPerformed

    private void RefreshbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RefreshbuttonActionPerformed
        refresh();
    }//GEN-LAST:event_RefreshbuttonActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GuruForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton Editbutton;
    private javax.swing.JToggleButton KeluarButton;
    private javax.swing.JButton Refreshbutton;
    private javax.swing.JTable TabelPresensi;
    private com.toedter.calendar.JDateChooser date_chooser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel jamfield;
    private javax.swing.JComboBox kelasCombo;
    // End of variables declaration//GEN-END:variables

    public void refresh() {
        try {
            Date tgl = this.date_chooser.getDate();
            SimpleDateFormat tglsimple = new SimpleDateFormat("dd-MM-yyyy");
            String tanggal = tglsimple.format(tgl);
            String kelas = kelasCombo.getSelectedItem().toString();
            List<Guru> gr = (List<Guru>) GuruKontrol.getKoneksi().getAll(tanggal, kelas);
            GuruTabelModel model = new GuruTabelModel(gr);
            TabelPresensi.setModel(model);
        } catch (Exception e) {
            Logger.getLogger(GuruTabelModel.class.getName()).log(Level.SEVERE, null, e);
        }
    }
}
